﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MicroFormatProcessing;

namespace MicroFormatWebRole
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void OnRegisterButtonClick(object sender, EventArgs e)
        {
            string userName = UsernameTextbox.Text;
            string password1 = Password1Textbox.Text;
            string password2 = Password2Textbox.Text;
            string fullName = FullNameTextBox.Text;
            string email = EmailTextBox.Text;
            string url = UrlTextBox.Text;

            if ((userName == null) || (userName.Length < 1))
            {
                ShowStatus("Missing Username!");
            }
            else if ((password1 == null) || (password1.Length < 1))
            {
                ShowStatus("Missing Password!");
            }
            else if (!password1.Equals(password2))
            {
                ShowStatus("Repeated Password different from Password!");
            }
            else if (UserUtil.CheckUser(userName, password1) != UserUtil.UNKNOWN_USER)
            {
                ShowStatus("That username is already taken!");
            }
            else if ((fullName == null) || (fullName.Length < 1))
            {
                ShowStatus("Full Name is missing!");
            }
            else if ((email == null) || (email.Length < 1))
            {
                ShowStatus("Email is missing!");
            }
            else
            {
                UserUtil.CreateUser(userName, password1);
                StringBuilder userUrl = new StringBuilder();
                userUrl.Append("Browse.aspx?user=").Append(userName);

                MicroFormatRepository mfRepository = new MicroFormatRepository();

                MicroFormat meCard = new MicroFormat();
                meCard.SetUserId(userName);
                StringBuilder foundOnUrl = new StringBuilder();
                foundOnUrl.Append(WebRole.BASE_URL).Append(userUrl.ToString());
                meCard.FoundOnUrl = foundOnUrl.ToString();
                meCard.FoundOnDate = DateTime.UtcNow;
                meCard.AddTag("Me");
                meCard.SetProperty("fn", fullName);
                meCard.SetProperty("email", email);
                if ((url != null) && (url.Length > 0))
                {
                    meCard.SetProperty("url", url);
                }
                meCard.Name = meCard.GetProperty("fn");
                meCard.Type = "hCard";
                meCard.SerializeAttributes();
                mfRepository.Add(meCard);

                TagRepository tagRepository = new TagRepository();
                
                Tag meTag = new Tag(meCard.GetUserId(), "Me");
                meTag.AddReference(meCard.RowKey);
                meTag.SerializeAttributes();
                tagRepository.Add(meTag);

                Tag friendsTag = new Tag(meCard.GetUserId(), "Friends");
                friendsTag.SerializeAttributes();
                tagRepository.Add(friendsTag);

                Session session = UserUtil.CreateSession(userName);
                Response.Cookies["session"]["id"] = session.GetSessionId();
                Response.Redirect(userUrl.ToString());
            }
        }

        private void ShowStatus(string status)
        {
            StatusLabel.Text = status;
            StatusSection.Visible = true;
        }
    }
}